<?php
/**
 * 
 * 测试人员控制器
 * @property Tester_mdl $tester
 * @property User_mdl $user
 * @property School_mdl $school
 */
class Tester extends Admin_Controller {
	public function __construct() {
		parent::__construct ();
		$this->load->model('Tester_mdl','tester');
		$this->load->model('User_mdl','user');
		$this->load->model('School_mdl','school');
	}
	
	public function index(){
		$schoolList = $this->getSchoolList();
		$this->load->library('Search_tpl',NULL,'searchTpl');
		$where = array('isTest'=>1);
		$where_in = array();
		$like  = array();
		$order = 'id';
		$sort  = 'DESC';
		$orders = array(
				'uid'=>'id',
				'time'=>'regTime',
		);
		$input = $this->input->get(NULL,TRUE);
		foreach ($input as $k=>$v){
			if ($v===''){
				continue;
			}
			$v = trim($v);
			switch ($k) {
				case 'uid':
					$where['id'] = (int)$v;
					break;
				case 'nickname':
					$like['nickname'] = $v;
					break;
				case 'order' :
					$order = isset($orders[$v]) ? $orders[$v] : 'id';
					break;
				case 'sort':
					$sort = (int)$v===0 ? 'DESC' : 'ASC';
					break;
				case 'phone':
					$where['phone'] = (float)$v;
					break;
				case 'showAllUser':
					unset($where['isTest']);
					break;
				case 'schoolID':
					$v = (int)$v;
					if( $v ){
						$like['schools'] = $v;
					}
			}
		}
		$query = array();
		$query['select']= '*';
		$query['where_in'] = $where_in;
		$query['from']  = MY_Model::T_USER;
		$query['join']  = array(
				'table'=>MY_Model::T_TESTER,
				'on'=>array('pk'=>'userID','fk'=>'id'),
				'type'=>'LEFT',
		);
		$query['where'] = $where;
		$query['like']  = $like;
		$query['order'] = $order;
		$query['sort']  = $sort;
		$list = $this->searchTpl->query($query);
		$temp = array();
		$temp['formItems'] = array(
				'uid',
				'nickname',
				'phone',
				'br',
				'school',
				'showAllUser',
		);
		$temp ['thList'] = array (
				array('text'=>'用户ID','order'=>'uid'),
				'用户昵称',
				'可用余额',
				'手机号码',
				'学校',
				'失效时间',
				'操作',
				
		);
		$callback = function($v)use($schoolList){
			$op='';
			if ($v['isTest']) {
				$op.='<a class="ibtn" href="'.ADMIN_DIR.'/tester/update?id='.$v['id'].'">修改</a>';
				$op.='<a class="ibtn" href="'.ADMIN_DIR.'/tester/revoke?id='.$v['id'].'">撤销</a>';
			}else{
				$op.='<a class="ibtn" href="'.ADMIN_DIR.'/tester/update?id='.$v['id'].'">设为调试人员</a>';
			}
			
			$schools = '';
			foreach (explode(',', $v['schools']) as $schoolID){
				$schools.= isset($schoolList[$schoolID]) ? $schoolList[$schoolID]['name'].';' : '';
			}
			$schools = rtrim($schools,';');
			
			return array(
					$v['id'],
					$v['nickname'],
					$v['balance'],
					$v['phone'],
					$schools,
					empty($v['invalidTime']) ? '' : date('Y-m-d H:i:s',$v['invalidTime']),
					$op,
			);
		};
		$temp['tableTitle'] = '用户列表--共查到%s个用户--';
		$temp['header'] = $this->getViewHeaderData();
// 		$temp['jsCtrl'] = 'userCtrl';
		$temp['schoolList'] = $schoolList;
		$this->searchTpl->show($temp,$callback);
	}
	
	
	
	public function add(){
		if (empty($_POST)) {
			$this->_showEdit();
			return ;
		}
		$schoolList  = $this->getSchoolList();
// 		$schoolID    = $this->input->post_get('schoolID',TRUE);
		$invalidTime = $this->input->post_get('invalidTime',TRUE);
		$balance     = $this->input->post_get('balance',TRUE);
		$phone       = $this->input->post_get('phone',TRUE);
		$nickname    = $this->input->post_get('nickname',TRUE);
		if( empty($nickname) ){
			$nickname = '调试人员';
		}
		$schools	 = $this->input->post_get('schools');
		if( ! empty($schools) && is_array($schools) ){
			$schools     = implode(',', $schools);
		}else{
			$schools = '';
		}
		// 检查该手机号码是否唯一
		$where = array();
		$where['phone'] = $phone;
		$user = $this->user->getOne($where);
		if( !empty($user) ){
// 			var_dump($user);die;
			alert('添加失败:该手机号码已经注册,请通过修改用户信息方式设为调试人员','back');
		}
		// 插入用户表
		$time = time();
		$user = array();
		$user['phone']    = $phone;
		$user['nickname'] = $nickname;
		$user['regTime']  = $time;
		$user['balance']  = $balance;
		$user['phoneID']  = $this->input->ip_address();
		$user['isTest']   = 1;
		$userID = $this->user->insert($user);
		$res = 0;
		if( $userID ){
			// 插入测试人员表
			$props = array();
			$props['userID']      = $userID;
			$props['schools']    = $schools;
			$props['invalidTime'] = strtotime($invalidTime);
			$this->tester->insert($props);
		}else{
			$res+=1;
		}
		
		if( $res===0 ){
			alert('添加成功,登陆名为:'.$phone,ADMIN_DIR.'tester/index');
		}else{
			alert('添加失败:'.$res,'back');
		}
	}
	
	public function update(){
		$userID = (int)$this->input->get_post('id');
		if ( !$userID ) {
			alert('错误操作','back');
			exit;
		}
		
		if ( empty($_POST) ) {
			$user = $this->user->getOneById($userID);
			if ($user['isTest']==1) {
				$where['userID'] = $userID;
				$props = $this->tester->getOne($where);
				if( ! empty($props) ){
					$props['schools'] = explode(',', $props['schools']);
					$user = array_merge($user,$props);
				}
			}
			$temp['user'] = $user;
// 			pr($temp);die;
			$this->_showEdit($temp);
			return ;
		}
		
// 		var_dump($_POST);DIE;
		
		$schoolID    = $this->input->post_get('schoolID',TRUE);
		$invalidTime = $this->input->post_get('invalidTime',TRUE);
		$balance     = $this->input->post_get('balance',TRUE);
		$nickname    = $this->input->post_get('nickname',TRUE);
		$schools	 = $this->input->post_get('schools');
		if( ! empty($schools) && is_array($schools) ){
			$schools     = implode(',', $schools);
		}else{
			$schools = '';
		}
		
		$error = 0;
		// 更新tester表
		$update = array();
// 		$tester['schoolID'] = $schoolID;
		$update['invalidTime'] = strtotime($invalidTime);
// 		$tester['userID'] = $userID;
		$update['schools'] = $schools;
		
		
		$where = array('userID'=>$userID);
		$tester = $this->tester->getOne($where) OR $error+=1;
		if( empty($tester) ){
			$update['userID'] = $userID;
			$this->tester->insert($update);
		}else{
			$this->tester->update($where,$update) OR $error+=2;
		}
// 		$sql = $this->db->last_query();
// 		$this->apiOutput->put('sql', $sql);
		
		
		// 更新user表
		$user = $this->user->create($userID);
		$user->nickname = $nickname;
		$user->balance  = $balance;
		$user->isTest   = 1;
		$user->update() OR $error+=4;
		
// 		$this->apiOutput->debug=true;
// 		$this->apiOutput->put('res', $error)->put('input', $_REQUEST)->put('where', $where)->put('update', $tester)->out();
		
		if( $error===0 ){
			alert('修改成功',ADMIN_DIR.'tester/index');
		}else{
			alert('修改失败:'.$error,'back');
		}
	}
	/**
	 * 取消调试人员
	 */
	public function revoke(){
		$userID = (int)$this->input->get_post('id');
		if ( empty($userID) ) {
			alert('无效操作','back');
			exit;
		}
		
		$user = $this->user->create($userID);
		$user->isTest = 0;
		$res = $user->update();
		$msg = '操作';
		$msg .= $res ? '成功' : '失败';
		alert($msg,'back');
		
	}
	
	private function _showEdit($temp=array()){
		$temp['schoolList'] = $this->getSchoolList();
		$this->showView('admin/tester/edit',$temp);
	}
	private function getSchoolList(){
		if( empty($this->schoolList) ){
			$schoolList = $this->school->getList();
			$this->schoolList = array_combine( array_column($schoolList, 'id') , $schoolList);
		}
		return $this->schoolList;
	}
}

